<template>
  <div>
    <!-- 头部导航 -->
    <div class="main">
      <van-nav-bar title="黑马头条" left-arrow @click-left="onClickLeft" />
    </div>
    <!-- 标题 -->
    <div class="navbar">
      <div class="font">{{ title }}</div>
    </div>
    <!-- 个人信息 -->
    <div class="myInfo">
      <van-cell :title="name" :label="pubdateA">
        <template #icon
          ><van-image
            round
            width="0.9rem"
            height="0.9rem"
            src="https://img01.yzcdn.cn/vant/cat.jpeg"
        /></template>
        <!-- 没关注 -->
        <van-button type="info" @click="userGua" v-if="!is_followed">
          <div v-if="ishowgun"><van-loading size="24" /></div>
          <div v-else><span class="span">+</span> 关注</div>
        </van-button>
        <van-button v-else class="guanhzu" type="info" @click="userGua">
          <div v-if="ishowgun"><van-loading size="24" /></div>
          <div v-else>已关注</div>
        </van-button>
      </van-cell>
    </div>
    <!-- 内容 -->
    <div v-html="content" class="markdown-body" @click="yulan"></div>
    <van-divider>正文结束</van-divider>

    <!-- 评论 -->
    <div class="pin">
      <div class="kuai" v-for="(itme, index) in resultsInfo" :key="index">
        <van-cell :title="itme.aut_name" :label="itme.content">
          <template #icon
            ><van-image
              round
              width="0.9rem"
              height="0.9rem"
              :src="itme.aut_photo"
          /></template>
          <span class="toutiao bottom toutiao-dianzan2">赞</span>
        </van-cell>
        <div class="color">
          <div style="display: inline-block">
            {{ pubdateB(itme.pubdate) }}&nbsp;&nbsp;&nbsp;
          </div>
          <div class="div" style="display: inline-block; margin-top: -3px">
            <van-button type="default" @click="changeisshow(itme)"
              ><span>回复</span><span>{{ itme.reply_count }}</span></van-button
            >
          </div>
        </div>
        <van-divider />
      </div>
      <p class="bottom">没有更多了</p>
    </div>

    <!-- 评论点击回复弹窗 -->
    <div class="conte">
      <van-popup v-model="show" position="bottom" :style="{ height: '100%' }">
        <pinInfo
          v-if="show"
          @ishow="show = false"
          :wzId="wenId"
          :myId="aut_id"
        ></pinInfo>
      </van-popup>
    </div>
    <!-- 评论按钮 -->
    <div class="pinlun">
      <van-row>
        <van-col span="12"
          ><van-button type="default" @click="isshowComment = true"
            >写评论</van-button
          ></van-col
        >
        <van-col span="3"
          ><span class="toutiao toutiao-pinglun"></span
        ></van-col>
        <van-col v-if="!is_collected" span="3" @click="shoucangA"
          ><span class="toutiao toutiao-shoucang"></span
        ></van-col>
        <van-col v-else span="3" @click="shoucangA"
          ><span class="toutiao shoucang toutiao-shoucang"></span
        ></van-col>
        <van-col span="3"
          ><span class="toutiao toutiao-dianzan"></span
        ></van-col>
        <van-col span="3"
          ><span class="toutiao toutiao-fenxiang"></span
        ></van-col>
      </van-row>
    </div>
    <!-- 评论底部弹出 -->
    <div class="footer">
      <van-popup
        v-model="isshowComment"
        position="bottom"
        :style="{ height: '30%' }"
      >
        <div class="divleft">
          <van-field
            v-model.trim="message"
            rows="2"
            :autosize="auto"
            type="textarea"
            maxlength="50"
            placeholder="请输入留言"
            show-word-limit
          />
        </div>
        <div class="divright" @click="isconten">发布</div>
      </van-popup>
    </div>
    <!-- 评论结束 -->
  </div>
</template>

<script>
import { ImagePreview } from 'vant'
import '@/views/home/Info/github-markdown.css'
import dayjs from '@/utils/day'
import pinInfo from './pinInfo.vue'
import {
  GetNewcApi,
  oidGuanApi,
  userGuanApi,
  useInfoApi,
  GetCommentsAppi,
  pinCommentsApi,
  collectionApi,
  oidcollectionaApi
} from '@/api'
export default {
  components: { pinInfo },
  watch: {
    isshowComment() {
      this.message = ''
    }
  },
  async created() {
    const {
      data: { data }
    } = await GetNewcApi(this.$route.params.name)
    //获取用户的资料
    const aa = await useInfoApi()
    //文章的评论
    const bb = await GetCommentsAppi('a', this.$route.params.name)
    this.resultsInfo = bb.data.data.results
    this.useaMy = aa.data.data
    this.name = data.aut_name
    this.photo = data.aut_photo
    this.title = data.title
    this.id = data.aut_id
    this.content = data.content
    this.is_followed = data.is_followed
    this.pubdate = data.pubdate
    this.is_collected = data.is_collected
  },
  data() {
    return {
      name: '',
      photo: '',
      title: '',
      content: '',
      show: false,
      is_followed: '',
      ishowgun: false,
      pubdate: '',
      useaMy: '',
      isshowComment: false,
      //当前输入的值
      message: '',
      auto: { maxHeight: 100, minHeight: 80 },
      //评论的数据
      resultsInfo: [],
      wenId: this.$route.params.name,
      id: '',
      aut_id: '',
      //收藏
      is_collected: ''
    }
  },
  computed: {
    pubdateA() {
      const a = dayjs(new Date())
      return dayjs(this.pubdate).from(a)
    },
    onload() {
      const imgReg = /<img.*?(?:>|\/>)/gi //匹配图片中的img标签
      const srcReg = /src=['"]?([^'"]*)['"]?/i // 匹配图片中的src
      const arr = this.content.match(imgReg) //筛选出所有的img
      const srcArr = []
      for (let i = 0; i < arr.length; i++) {
        const src = arr[i].match(srcReg)
        // 获取图片地址
        srcArr.push(src[1])
      }
      return srcArr
    }
  },
  methods: {
    pubdateB(c) {
      const a = dayjs(new Date())
      return dayjs(c).from(a)
    },
    onClickLeft() {
      this.$router.push('/ ')
    },
    async userGua() {
      this.ishowgun = true
      try {
        if (!this.is_followed) {
          await userGuanApi(this.id)
          this.is_followed = true
        } else {
          await oidGuanApi(this.id)
          this.is_followed = false
        }
        this.ishowgun = false
      } catch (error) {}
    },

    //点击发布评论
    async isconten() {
      try {
        this.$toast.loading({
          message: '加载中...',
          forbidClick: true
        })
        const a = this.wenId
        const b = this.message
        if (b === '') return
        await pinCommentsApi(a, b)
        const bb = await GetCommentsAppi('a', this.wenId)
        this.resultsInfo = bb.data.data.results
        this.isshowComment = false
        this.$toast.success('评论成功')
      } catch (error) {}
    },
    changeisshow(itme) {
      this.show = true
      this.aut_id = itme
    },
    //收藏
    async shoucangA() {
      try {
        if (!this.is_collected) {
          this.$toast.loading({
            message: '收藏中...',
            forbidClick: true
          })
          await collectionApi(this.wenId)
          this.is_collected = true
          this.$toast.success('收藏成功')
        } else {
          this.$toast.loading({
            message: '取消收藏...',
            forbidClick: true
          })
          await oidcollectionaApi(this.wenId)
          this.is_collected = false
          this.$toast.success('取消成功')
        }
      } catch (error) {}
    },
    //点击图片
    yulan(e) {
      if (e.target.nodeName === 'IMG') {
        ImagePreview({
          images: this.onload,
          startPosition: this.onload.indexOf(e.target.src),
          closeOnPopstate: false
        })
      }
    }
  }
}
</script>

<style scoped lang="less">
:deep(.main) {
  position: fixed;
  width: 100%;
  top: 0;
  z-index: 10000000;
  .van-nav-bar {
    background-color: #3296fa;
  }
  .van-icon {
    color: #fff;
  }
  .van-nav-bar__title {
    color: #fff;
  }
}
.navbar {
  margin-top: 92px;
  overflow: hidden;
  .font {
    font-size: 60px;
    color: #3a3a3a;
    padding: 0.66667rem 0.42667rem;
  }
}
:deep(.guanhzu) {
  color: #111;
  border: 1px solid #ccc;
  background-color: #fff !important;
}
:deep(.myInfo) {
  .van-image {
    margin-top: 10px;
    margin-right: 7px;
  }
  .van-button {
    margin-top: 13px;
    width: 2.26667rem;
    height: 0.77333rem;
    border-radius: 26.64rem;
  }
  .span {
    font-size: 30px;
  }
}
.content {
  width: 100%;
  font-size: 20px;
  margin-left: 10px;
  margin-right: 50px;
  width: 100%;
  overflow: hidden;
}
.bottom {
  font-size: 26px;
  color: #7a7a7a;
  text-align: center;
}
:deep(.pin) {
  margin-bottom: 140px;
  .van-cell::after {
    border-bottom: unset;
  }
  .van-image {
    margin-top: 10px;
    margin-right: 7px;
  }
  .span {
    font-size: 30px;
  }
  .color {
    margin-left: 104px;
    font-size: 24px;
    height: 40px;
    line-height: 40px;
    display: flex;
    align-items: center;
  }
  .van-button {
    width: 100%;
    height: 100%;
    width: 125px;
    height: 40px;
    font-size: 12px;
    text-align: center;
    border-radius: 40px;
  }
  .van-button--normal {
    padding: unset;
  }
}
:deep(.pinlun) {
  background-color: #fff;
  width: 100%;
  position: fixed;
  bottom: 0;
  height: 90px;
  border-top: 1px solid #ccc;
  .shoucang {
    color: red;
  }
  .toutiao {
    font-size: 36px;
    line-height: 90px;
  }
  .van-button {
    width: 330px;
    height: 50px;
    border-radius: 30px;
    margin-left: 18px;
  }
}
:deep(.conte) {
  position: relative;
  z-index: 100000000000000000;
}
:deep(.footer) {
  .divleft {
    display: inline-block;
    width: 80%;
    margin: 50px 0 0 34px;
    overflow: hidden;
  }
  .van-cell {
    border: 1px solid #ccc;
    background-color: #f5f7f9;
    border-radius: 20px;
  }
  .divright {
    display: inline-block;
    font-size: 30px;
    color: #b5d1ec;
    margin-left: 25px;
    position: relative;
    top: -100px;
  }
}
.markdown-body {
  box-sizing: border-box;
  min-width: 200px;
  max-width: 980px;
  margin: 0 auto;
  padding: 45px;
}

@media (max-width: 767px) {
  .markdown-body {
    padding: 15px;
  }
}
</style>
